package com.hzit.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class MyBatisUtil {
	//1. 得到一个SqlSessionFactory对象
	private static SqlSessionFactory sqlSessionFactory() throws IOException {
		String resource = "mybatis/mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;
	}
	//2. 得到sqlSession对象,相当于jdbc中的connection
	public static SqlSession getSession()  {
		//2.1 得到SqlSessionFactory
		try {
			SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
			return sqlSessionFactory.openSession(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
